
import java.util.Scanner;

public class CashRegister {

    public static final int PRICE_FOR_APPLE  = 1;
    public static final int PRICE_FOR_MANGO  = 2;
    public static final int PRICE_FOR_BANANA = 3;
    public static final int PRICE_FOR_ORANGE = 4;

    public static int store_balance = 100;
    public static int customer_balance = 100;
    public static Scanner scan = new Scanner(System.in);
    public static boolean isSelecting = true;
    public static int appleCount = 0;  public static int mangoCount = 0; public static int bananaCount = 0; public static int orangeCount = 0;
    public static int itemNumber = 0; public static int quantity = 0;
    public static int totalBill = 0;
    public static Store store1 = new Store("eFreshFruits", store_balance);
    public static Customer customer1 = new Customer("Marvin Martian", customer_balance);

    public static void main(String[] args) {
        System.out.println("Max: " + Integer.MAX_VALUE);
        System.out.println("Min: " + Integer.MIN_VALUE);
        System.out.println("Welcome to eFreshFruits store, " + customer1.getName() + "!");
        System.out.println("Initial Customer Balance: $" + customer1.getBalance());
        System.out.println("Initial Store Balance   : $" + store1.getBalance());

        while(isSelecting) {
            System.out.println("\nEnter the the item number and the amount you want to purchase.");
            System.out.println("Item 1: Apples  - $1.0 ea");
            System.out.println("Item 2: Mangoes - $2.0 ea");
            System.out.println("Item 3: Banana  - $3.0 ea");
            System.out.println("Item 4: Orange  - $4.0 ea");

            System.out.println("\nEnter Item number: ");
            itemNumber = scan.nextInt();
            System.out.println("Enter quantity: ");
            quantity = scan.nextInt();
            switch(itemNumber) {
                case 1:
                    appleCount += quantity; break;
                case 2:
                    mangoCount += quantity; break;
                case 3:
                    bananaCount += quantity; break;
                case 4:
                    orangeCount += quantity; break;
                default: System.out.println("\nSorry, the item is not available!"); break;
            }

            System.out.println("Do you want to buy more? y/n");
            String operator = scan.next();
            if(operator.equals("y")){ isSelecting = true; }
            else                       { isSelecting = false; }
        }

        totalBill = appleCount * PRICE_FOR_APPLE +
                      mangoCount * PRICE_FOR_MANGO +
                      bananaCount * PRICE_FOR_BANANA +
                      orangeCount * PRICE_FOR_ORANGE;
        
        chargeCustomer(customer1, totalBill);
        depositMoneyToStore(store1, totalBill);
        System.out.println("\nTotal purchase : $" + totalBill);
        System.out.println("Customer Balance = $" + customer1.getBalance());
        System.out.println("Store Balance    = $" + store1.getBalance());
    }

    public static void chargeCustomer(Customer customer, int bill) {
        customer.balance = customer.balance - bill;
    }

    public static void depositMoneyToStore(Store store, int amount) {
        store.balance = store.balance + amount;
    }
}